package top.drinyeit.nongmao.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;

import com.trello.rxlifecycle2.components.support.RxFragment;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.dialog.LoadDialog;
import top.drinyeit.nongmao.util.DensityUtil;

/**
 * Created by Administrator on 2017/11/30.
 */

public abstract class LazyFragment extends RxFragment implements IBaseView {

    private LoadDialog loadDialog;
    protected Context mContext;
    protected View mRootView;
    private boolean mIsMulti = false;
    int statusBarHeight1 = -1;//状态栏高度

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxBus.get().register(this);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(attachLayoutRes(), container, false);
            initViews();
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getUserVisibleHint() && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            lazyLoad();
        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isVisible() && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            lazyLoad();
        } else {
            super.setUserVisibleHint(isVisibleToUser);
        }
    }

    /**
     * 绑定布局文件
     *
     * @return 布局文件ID
     */
    protected abstract int attachLayoutRes();


    /**
     * 初始化视图控件
     */
    protected abstract void initViews();

    /**
     * 当视图初始化并且对用户可见的时候去真正的加载数据
     */
    protected abstract void lazyLoad();

    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以覆写此方法
     */
    protected void stopLoad() {

    }

    @Override
    public void showToastMsg(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(getContext());
        }
        loadDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (loadDialog != null) {
            loadDialog.hide();
        }
    }

    @Override
    public void onDestroy() {
        if (loadDialog != null) {
            loadDialog.dismiss();
            loadDialog = null;
        }
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    //设置状态栏高度
    public void setTitle(boolean isBack,String t) {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelOffset(resourceId);
        }
        Log.e("WangJ", "状态栏-高度:" + statusBarHeight1);

        View state = mRootView.findViewById(R.id.v_state);
        LinearLayout back = mRootView.findViewById(R.id.ll_back);
        TextView title = mRootView.findViewById(R.id.tv_title);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            state.setBackgroundColor(ContextCompat.getColor(this.getActivity(),R.color.transparent_03));
        }else {
            state.setBackgroundColor(ContextCompat.getColor(this.getActivity(),R.color.transparent));
        }

        Log.e("Height", "标题栏高度：" + DensityUtil.dip2px(this.getActivity(), 44));
        if (state.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams linearParams1 = (LinearLayout.LayoutParams) state.getLayoutParams();
            linearParams1.height = statusBarHeight1;
        }else if(state.getLayoutParams() instanceof FrameLayout.LayoutParams){
            FrameLayout.LayoutParams linearParams1 = (FrameLayout.LayoutParams) state.getLayoutParams();
            linearParams1.height = statusBarHeight1;
        }

        if (isBack){
            back.setVisibility(View.VISIBLE);
        }else {
            back.setVisibility(View.GONE);
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        back.setVisibility(View.GONE);

        title.setText(t);

    }

    public void setTitle(boolean isBack){
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelOffset(resourceId);
        }
        Log.e("WangJ", "状态栏-高度:" + statusBarHeight1);

        View state = mRootView.findViewById(R.id.v_state);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            state.setBackgroundColor(ContextCompat.getColor(this.getActivity(),R.color.transparent_03));
        }else {
            state.setBackgroundColor(ContextCompat.getColor(this.getActivity(),R.color.transparent));
        }

    }
}
